package com.wanmei.controller.product;

import com.github.pagehelper.PageInfo;
import com.wanmei.commons.ResultUtils;
import com.wanmei.pojo.Product;
import com.wanmei.pojo.Result;
import com.wanmei.service.Impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("product")
public class ProductListController {
    @Autowired
    private ProductServiceImpl productService;
    /**
     * 商品列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/listProduct")
    public ModelAndView listProduct(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        ModelAndView mv = new ModelAndView();
        PageInfo<Object> pageInfo = productService.showProduct(pageNum,pageSize);
        List<Object> productkuidList=productService.selectProductKuid();
        System.out.println("库存信息"+productkuidList);
        mv.addObject("productKuidList",productkuidList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("/product/productList");
        return mv;
    }
    /**
     * 倒序查询，添加商品后跳转到此页面
     */
    @RequestMapping("/listProductDESC")
    public ModelAndView listProductDESC(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        ModelAndView mv = new ModelAndView();
        PageInfo<Object> pageInfo = productService.showProductDESC(pageNum,pageSize);
        List<Object> productkuidList=productService.selectProductKuid();
        mv.addObject("productKuidList",productkuidList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("/product/productList");
        return mv;
    }
    /**
     * 模糊查询
     */
    @RequestMapping("/listFuzzyProduct")
    public ModelAndView listProductDESC(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, HttpServletRequest req){
        String likeValue = req.getParameter("likeValue");
        ModelAndView mv = new ModelAndView();
        PageInfo<Product> pageInfo = productService.getProductByFuzzy( likeValue,pageNum, pageSize);
        req.setAttribute("likeValue",likeValue);
        List<Object> productkuidList=productService.selectProductKuid();
        mv.addObject("productKuidList",productkuidList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("product/productList");
        return mv;

    }

}
